﻿// Copyright (c) ZhangPeihang. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

namespace LaserMark.Control.Service
{
    public class GTControl : IControl
    {
        public Task<short> AxisOff(short cardNo, short axis) => Task.FromResult(mc.GT_AxisOff(cardNo, axis));

        public Task<short> SetDoBit(short cardNo, short doType, short doIndex, short dValue)
        {
            var ret = mc.GT_SetDoBit(cardNo, doType, doIndex, dValue);
            return Task.FromResult(ret);
        }

        public Task<short> AxisOn(short cardNo, short axis) => Task.FromResult(mc.GT_AxisOn(cardNo, axis));

        public Task<short> ClearAxisStatus(short cardNo, short axis, short count)
        {
            var ret = mc.GT_ClrSts(cardNo, axis, count);
            return Task.FromResult(ret);
        }

        public Task<short> CloseControl(short cardNo)
        {
            var ret = mc.GT_Close(cardNo);
            return Task.FromResult(ret);
        }

        public Task<short> GetAxisStatus(short cardNo, short axis, out int pSts, short count, out uint pClock)
        {
            var ret = mc.GT_GetSts(cardNo, axis, out pSts, count, out pClock);

            return Task.FromResult(ret);
        }

        public Task<short> GetDi(short cardNo, short diType, out int dValue)
        {
            var ret = mc.GT_GetDi(cardNo, diType, out dValue);

            return Task.FromResult(ret);
        }

        public Task<short> LoadConfig(short cardNo, string path)
        {
            short ret = 404;
            try
            {
                ret = mc.GT_LoadConfig(cardNo, path);
                return Task.FromResult(ret);
            }
            catch (Exception) { }

            return Task.FromResult(ret);
        }

        public Task<short> Open(short cardNo, short channel, short param)
        {
            var ret = mc.GT_Open(cardNo, channel, param);
            return Task.FromResult(ret);
        }

        public Task<short> Rest(short cardNo)
        {
            var ret = mc.GT_Reset(cardNo);
            return Task.FromResult(ret);
        }

        public Task<short> SetDo(short cardNo, short doType, int dValue)
        {
            var ret = mc.GT_SetDo(cardNo, doType, dValue);
            return Task.FromResult(ret);

        }

        public Task<short> SetPrfJog(short cardNo, short axis, int vel, ref mc.TJogPrm jogPrm)
        {
            mc.GT_PrfJog(cardNo, axis);
            mc.GT_GetJogPrm(cardNo, axis, out var ppm);
            ppm.acc = jogPrm.acc;
            ppm.dec = jogPrm.dec;
            ppm.smooth = jogPrm.smooth;
            mc.GT_SetJogPrm(cardNo, axis, ref ppm);
            mc.GT_SetVel(cardNo, axis, vel);
            mc.GT_Update(cardNo, 1 << (axis - 1));

            return Task.FromResult((short)0);
        }

        public Task<short> SetPTP(short cardNo, short profile, int vel, double pos, ref mc.TTrapPrm trapPrm)
        {
            mc.GT_PrfTrap(cardNo, profile);
            mc.GT_GetTrapPrm(cardNo, profile, out var ppm);
            ppm.acc = trapPrm.acc;
            ppm.dec = trapPrm.dec;
            ppm.smoothTime = trapPrm.smoothTime;
            mc.GT_SetTrapPrm(cardNo, profile, ref ppm);
            mc.GT_SetVel(cardNo, profile, vel);
            mc.GT_SetPos(cardNo, profile, (int)pos);
            mc.GT_Update(cardNo, 1 << (profile - 1));

            return Task.FromResult((short)0);
        }

        public Task<short> SetSoftLimit(short cardNo, short axis, int forward, int reserse)
        {
            var ret = mc.GT_SetSoftLimit(cardNo, axis, forward, reserse);

            return Task.FromResult(ret);
        }

        public Task<short> Stop(short cardNo, int mask, int count)
        {
            var ret = mc.GT_Stop(cardNo, mask, count);
            return Task.FromResult(ret);
        }

        public Task<short> GT_GetPrfMode(short cardNo, short profile, out int type, short count)
        {
            var ret = mc.GT_GetPrfMode(cardNo, profile, out type, count, out _);
            return Task.FromResult(ret);
        }

        public Task<short> ZeroPos(short cardNo, short axis, short count)
        {
            var ret = mc.GT_ZeroPos(cardNo, axis, count);
            return Task.FromResult(ret);
        }
    }
}
